//
//  STNewFunctionView.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/13.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

import SVProgressHUD

let kScreen_B = UIScreen.mainScreen().bounds
let kScreen_W = UIScreen.mainScreen().bounds.width
let kScreen_H = UIScreen.mainScreen().bounds.height

class STNewFunctionView: UIView {
    // MARK: - 懒加载 scrollView
    lazy var scrollView:UIScrollView={
        let slideView = UIScrollView()
        slideView.pagingEnabled=true
        slideView.bounces=false
        slideView.showsVerticalScrollIndicator=false
//        slideView.userInteractionEnabled = true
        for i in 0..<imageCount{
            
            let iconView = UIImageView(image: UIImage(named: String(format: "new_feature_%d", i+1)))
            iconView.frame = CGRect(x: kScreen_W*CGFloat(i), y: 0, width: kScreen_W, height: kScreen_H)
            
            slideView.addSubview(iconView)
            
            if i==imageCount-1 {
                
                iconView.userInteractionEnabled = true
                iconView.addSubview(self.startButton)
            }
        }
        slideView.contentSize=CGSize(width: kScreen_W*CGFloat(imageCount), height: kScreen_H)
        slideView.contentOffset=CGPoint(x: 0, y: kScreen_H)
        return slideView
    }()
    // MARK: - 懒加载 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.bounds=CGRect(x: 0, y: 0, width: 100, height: 25)
        button.center=CGPoint(x: kScreen_W*0.5, y: kScreen_H-160)
        // 设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: Selector("startPlay"), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.hidden=true
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 更新UI
    private func setupUI(){
        scrollView.frame = kScreen_B
        self.addSubview(scrollView)
    }
    
    /// 开始按钮动画
    func startAnimation() {
        // 动画之前,将按钮显示出来
        startButton.hidden = false
        // 在动画前设置transform.scale为0.看不到
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 恢复到原来的大小
            self.startButton.transform = CGAffineTransformMakeScale(1, 1) // 清空transform到默认的状态 scale = 1
            }, completion: nil)
    }
    
    func hiddenStartButton(){
        startButton.hidden = true
    }
    
    @objc private func startPlay(){
        STPrint(items: "开始体验")

        SVProgressHUD.showWithStatus("正在载入，请稍后...")
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue()) { () -> Void in
            SVProgressHUD.dismiss()
            (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootVC(STTabBarVC())
        }
    }
    
    
}
